package demo2;

import java.util.HashMap;
import java.util.Map;

public class LeetCode525 {
    public int findMaxLength(int[] nums) {
        int sum = 0, n = nums.length;
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, -1);
        int max_lens = 0;
        for (int i = 0; i < n; i++) {
            sum += (nums[i] == 0 ? -1 : 1);
            if (hash.containsKey(sum)) max_lens = Math.max(max_lens, i - hash.get(sum));
            else hash.put(sum, i);
        }
        return max_lens;
    }
}
